Conditions | 1 |
Paths | 1 |
Total Lines | 153 |
Code Lines | 90 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | define(['map/clientlayer', 'map/labellayer', 'leaflet', 'map/locationmarker'], |
||
2 | function (ClientLayer, LabelLayer, L, LocationMarker) { |
||
3 | 'use strict'; |
||
4 | var self = {}; |
||
5 | |||
6 | var ButtonBase = L.Control.extend({ |
||
7 | options: { |
||
8 | position: 'bottomright' |
||
9 | }, |
||
10 | |||
11 | active: false, |
||
12 | button: undefined, |
||
13 | |||
14 | initialize: function (f, o) { |
||
15 | L.Util.setOptions(this, o); |
||
16 | this.f = f; |
||
17 | }, |
||
18 | |||
19 | update: function () { |
||
20 | this.button.classList.toggle('active', this.active); |
||
21 | }, |
||
22 | |||
23 | set: function (v) { |
||
24 | this.active = v; |
||
25 | this.update(); |
||
26 | } |
||
27 | }); |
||
28 | |||
29 | var LocateButton = ButtonBase.extend({ |
||
30 | onAdd: function () { |
||
31 | var button = L.DomUtil.create('button', 'ion-locate'); |
||
32 | button.setAttribute('aria-label', _.t('button.tracking')); |
||
33 | L.DomEvent.disableClickPropagation(button); |
||
34 | L.DomEvent.addListener(button, 'click', this.onClick, this); |
||
35 | |||
36 | this.button = button; |
||
37 | |||
38 | return button; |
||
39 | }, |
||
40 | |||
41 | onClick: function () { |
||
42 | this.f(!this.active); |
||
43 | } |
||
44 | }); |
||
45 | |||
46 | var CoordsPickerButton = ButtonBase.extend({ |
||
47 | onAdd: function () { |
||
48 | var button = L.DomUtil.create('button', 'ion-pin'); |
||
49 | button.setAttribute('aria-label', _.t('button.location')); |
||
50 | |||
51 | // Click propagation isn't disabled as this causes problems with the |
||
52 | // location picking mode; instead propagation is stopped in onClick(). |
||
53 | L.DomEvent.addListener(button, 'click', this.onClick, this); |
||
54 | |||
55 | this.button = button; |
||
56 | |||
57 | return button; |
||
58 | }, |
||
59 | |||
60 | onClick: function (e) { |
||
61 | L.DomEvent.stopPropagation(e); |
||
62 | this.f(!this.active); |
||
63 | } |
||
64 | }); |
||
65 | |||
66 | return function (map, buttons) { |
||
67 | var userLocation; |
||
68 | |||
69 | var locateUserButton = new LocateButton(function (d) { |
||
70 | if (d) { |
||
71 | enableTracking(); |
||
72 | } else { |
||
73 | self.disableTracking(); |
||
74 | } |
||
75 | }); |
||
76 | |||
77 | var mybuttons = []; |
||
78 | |||
79 | function addButton(button) { |
||
80 | var el = button.onAdd(); |
||
81 | mybuttons.push(el); |
||
82 | buttons.appendChild(el); |
||
83 | } |
||
84 | |||
85 | self.clearButtons = function clearButtons() { |
||
86 | mybuttons.forEach(function (d) { |
||
87 | buttons.removeChild(d); |
||
88 | }); |
||
89 | }; |
||
90 | |||
91 | var showCoordsPickerButton = new CoordsPickerButton(function (d) { |
||
92 | if (d) { |
||
93 | enableCoords(); |
||
94 | } else { |
||
95 | disableCoords(); |
||
96 | } |
||
97 | }); |
||
98 | |||
99 | function enableTracking() { |
||
100 | map.locate({ |
||
101 | watch: true, |
||
102 | enableHighAccuracy: true, |
||
103 | setView: true |
||
104 | }); |
||
105 | locateUserButton.set(true); |
||
106 | } |
||
107 | |||
108 | self.disableTracking = function disableTracking() { |
||
109 | map.stopLocate(); |
||
110 | self.locationError(); |
||
111 | locateUserButton.set(false); |
||
112 | }; |
||
113 | |||
114 | function enableCoords() { |
||
115 | map.getContainer().classList.add('pick-coordinates'); |
||
116 | map.on('click', showCoordinates); |
||
117 | showCoordsPickerButton.set(true); |
||
118 | } |
||
119 | |||
120 | function disableCoords() { |
||
121 | map.getContainer().classList.remove('pick-coordinates'); |
||
122 | map.off('click', showCoordinates); |
||
123 | showCoordsPickerButton.set(false); |
||
124 | } |
||
125 | |||
126 | function showCoordinates(e) { |
||
127 | router.fullUrl({ zoom: map.getZoom(), lat: e.latlng.lat, lng: e.latlng.lng }); |
||
128 | disableCoords(); |
||
129 | } |
||
130 | |||
131 | self.locationFound = function locationFound(e) { |
||
132 | if (!userLocation) { |
||
133 | userLocation = new LocationMarker(e.latlng).addTo(map); |
||
134 | } |
||
135 | |||
136 | userLocation.setLatLng(e.latlng); |
||
137 | userLocation.setAccuracy(e.accuracy); |
||
138 | }; |
||
139 | |||
140 | self.locationError = function locationError() { |
||
141 | if (userLocation) { |
||
142 | map.removeLayer(userLocation); |
||
143 | userLocation = null; |
||
144 | } |
||
145 | }; |
||
146 | |||
147 | self.init = function init() { |
||
148 | addButton(locateUserButton); |
||
149 | addButton(showCoordsPickerButton); |
||
150 | }; |
||
151 | |||
152 | return self; |
||
153 | }; |
||
154 | }); |
||
155 |